package com.xh.pmo.actions;



import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.xh.pmo.base.ModelDrivenBaseAction;
import com.xh.pmo.domain.Team;
import com.xh.pmo.domain.User;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class UserAction extends ModelDrivenBaseAction<User>{
	
	public String registerUI () {
		return "registerUI";
	}
	
	public String register() {

		User u = new User ();
		
		u.setUsername("yao");
		u.setRegisterTime(new Date());
		userService.save(u);
		System.out.println(u.getId());
		
		
		Team team = new Team();
		team.setLinkman(u.getUsername());
		team.setCreateTime(new Date());
		team.setName(u.getUsername()+"的团队");
		teamService.save(team);
		
		
		List<User> users = userService.findAll();
		System.out.println(users.size());
		return "registerUI";
	}
	
	@SuppressWarnings("unused")
	public String login (){
		
		User user = userService.getUserByEmailAndPassword(model.getEmail(),model.getPassword());
		if(user != null) {
			ActionContext.getContext().getSession().put("user", user);
			return "toIndex";
		} else {
			addFieldError("error", "用户名或者密码不正确");
			return "loginUI";
		}
		
	}
	
	public String loginOut() {
		ActionContext.getContext().getSession().remove("user");
		return "loginUI";
	}
	
	public String index () {
		
		return "index";
	} 
	
	
	public String loginUI () {
		
		return "loginUI";
	}
}
